<?php namespace WoXuanWang\Video\Controllers;

use WoXuanWang\Video\Requests\CreateSourceRequest;
use WoXuanWang\Video\Requests\UpdateSourceRequest;
use App\Http\Requests\IndexRequest;
use App\Http\Requests\ShowRequest;
use Illuminate\Http\JsonResponse;
use WoXuanWang\Video\Repositories\SourceRepository;
use zgldh\Scaffold\AppBaseController;

class SourceController extends AppBaseController
{

    public function __construct(SourceRepository $sourceRepository)
    {
        $this->repository = $sourceRepository;
        $this->middleware("auth");
    }

    /**
     * Display a listing of the Source.
     *
     * @param Request $request
     * @return Response
     */
    public function index(IndexRequest $request)
    {
          $with = $request->getWith();
          $data = $this->repository->datatables(null, $with);
          return $data;
    }

    /**
     * Store a newly created Source in storage.
     *
     * @param CreateSourceRequest $request
     * @return JsonResponse
     */
    public function store(CreateSourceRequest $request)
    {
        $input = $request->all();

        $source = $this->repository->create($input);

        return $this->sendResponse($source, 'Source saved successfully.');
    }

    /**
     * Display the specified Source.
     *
     * @param  int $id
     *
     * @return JsonResponse
     */
    public function show($id, ShowRequest $request)
    {
        $this->repository->with($request->getWith());
        $source = $this->repository->findWithoutFail($id);

        if (empty($source)) {
            return $this->sendError('Source not found');
        }

        return $this->sendResponse($source, '');
    }

    /**
     * Update the specified Source in storage.
     *
     * @param  int $id
     * @param UpdateSourceRequest $request
     *
     * @return JsonResponse
     */
    public function update($id, UpdateSourceRequest $request)
    {
        $source = $this->repository->findWithoutFail($id);

        if (empty($source)) {
            return $this->sendError('Source not found');
        }

        $source = $this->repository->update($request->all(), $id);

        return $this->sendResponse($source, 'Source updated successfully.');
    }

    /**
     * Remove the specified Source from storage.
     *
     * @param  int $id
     *
     * @return JsonResponse
     */
    public function destroy($id)
    {
        $source = $this->repository->findWithoutFail($id);

        if (empty($source)) {
            return $this->sendError('Source not found');
        }

        $this->repository->delete($id);
        return $this->sendResponse($source, 'Source deleted successfully.');
    }
}
